<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>moment时间处理相关小结 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="JavaScript,moment,">
  

  <meta name="description" content="一、基础用法取当天时间，以YYYY年MM月DD日形式显示 var now=moment().format(&quot;YYYY年MM月DD日&quot;); 任意时间戳格式化，以YYYY-MM-DD HH:mm:ss形式显示 var t1=moment(1411641720000).format(&apos;YYYY-MM-DD HH:mm:ss&apos;); 获取前一天日期，格式以YYYY-MM-DD形式显示  var t11=mo">
<meta name="keywords" content="JavaScript,moment">
<meta property="og:type" content="article">
<meta property="og:title" content="moment时间处理相关小结">
<meta property="og:url" content="http://blog.poetries.top/2018/12/06/moment-api/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="一、基础用法取当天时间，以YYYY年MM月DD日形式显示 var now=moment().format(&quot;YYYY年MM月DD日&quot;); 任意时间戳格式化，以YYYY-MM-DD HH:mm:ss形式显示 var t1=moment(1411641720000).format(&apos;YYYY-MM-DD HH:mm:ss&apos;); 获取前一天日期，格式以YYYY-MM-DD形式显示  var t11=mo">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-08-15T04:25:31.926Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="moment时间处理相关小结">
<meta name="twitter:description" content="一、基础用法取当天时间，以YYYY年MM月DD日形式显示 var now=moment().format(&quot;YYYY年MM月DD日&quot;); 任意时间戳格式化，以YYYY-MM-DD HH:mm:ss形式显示 var t1=moment(1411641720000).format(&apos;YYYY-MM-DD HH:mm:ss&apos;); 获取前一天日期，格式以YYYY-MM-DD形式显示  var t11=mo">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、基础用法"><span class="toc-text">一、基础用法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、更多示例"><span class="toc-text">二、更多示例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三、综合"><span class="toc-text">三、综合</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四、其他"><span class="toc-text">四、其他</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#五、更多参考"><span class="toc-text">五、更多参考</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-moment-api" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">moment时间处理相关小结</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2018.12.06</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 979字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 5分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h2 id="一、基础用法"><a href="#一、基础用法" class="headerlink" title="一、基础用法"></a>一、基础用法</h2><p><strong>取当天时间，以YYYY年MM月DD日形式显示</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> now=moment().format(<span class="string">"YYYY年MM月DD日"</span>);</span><br></pre></td></tr></table></figure>
<p><strong>任意时间戳格式化，以<code>YYYY-MM-DD HH:mm:ss</code>形式显示</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> t1=moment(<span class="number">1411641720000</span>).format(<span class="string">'YYYY-MM-DD HH:mm:ss'</span>);</span><br></pre></td></tr></table></figure>
<p><strong>获取前一天日期，格式以<code>YYYY-MM-DD</code>形式显示</strong></p>
 <figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> t11=moment().day(<span class="number">0</span>).format(<span class="string">'YYYY-MM-DD'</span>);</span><br></pre></td></tr></table></figure>
<p><strong>获取本周五日期，格式以<code>YYYY-MM-DD</code>形式显示</strong></p>
 <figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> t12=moment().weekday(<span class="number">5</span>).format(<span class="string">'YYYY-MM-DD'</span>);</span><br></pre></td></tr></table></figure>
<p><strong>获取上周五日期，格式以<code>YYYY-MM-DD</code>形式显示</strong></p>
 <figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> t13=moment().weekday(<span class="number">-3</span>).format(<span class="string">'YYYY-MM-DD'</span>);</span><br></pre></td></tr></table></figure>
<p><strong>获取当前年份、月份、日期</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> t14=moment().year()</span><br><span class="line"><span class="keyword">var</span> t15=moment().month()<span class="comment">//此处月份从0开始，当前月要+1</span></span><br><span class="line"><span class="keyword">var</span> t16=moment().date();</span><br></pre></td></tr></table></figure>
<p><strong>获取上个月今天的日期，格式以YYYY-MM-DD显示</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> t18=moment().subtract(<span class="number">1</span>, <span class="string">'months'</span>).format(<span class="string">'YYYY-MM-DD'</span>);</span><br></pre></td></tr></table></figure>
<p><strong>获取上个月日期，格式以YYYY-MM显示</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> t19=moment().subtract(<span class="number">1</span>, <span class="string">'months'</span>).format(<span class="string">'YYYY-MM'</span>)</span><br></pre></td></tr></table></figure>
<p><strong>获取前一天日期，格式以YYYY-MM-DD显示</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> t20 = moment().subtract(<span class="number">1</span>, <span class="string">'days'</span>).format(<span class="string">'YYYY-MM-DD'</span>);</span><br></pre></td></tr></table></figure>
<p><strong>获取两个小时之后的时间</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> t22=moment().add(<span class="number">2</span>,<span class="string">'hours'</span>).format(<span class="string">'YYYY-MM-DD HH:mm:ss'</span>);</span><br></pre></td></tr></table></figure>
<p><strong>获取五天前的日期</strong></p>
<ul>
<li>例如:今天<code>2018-7-23</code>，获取到的时间是<code>2018-7-18</code></li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> t23=moment().subtract(<span class="number">5</span>, <span class="string">'days'</span>).format(<span class="string">'YYYY-MM-DD'</span>);</span><br></pre></td></tr></table></figure>
<h2 id="二、更多示例"><a href="#二、更多示例" class="headerlink" title="二、更多示例"></a>二、更多示例</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="meta">'use strict'</span></span><br><span class="line"><span class="keyword">let</span> moment = <span class="built_in">require</span>(<span class="string">'moment'</span>);</span><br><span class="line"><span class="comment">//获取当前时间</span></span><br><span class="line"><span class="keyword">var</span> now = moment().toDate();</span><br><span class="line"><span class="built_in">console</span>.log(now)</span><br><span class="line"> </span><br><span class="line"><span class="comment">//格式化当前时间</span></span><br><span class="line">now = moment().format(<span class="string">'YYYY-MM-DD'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(now);</span><br><span class="line"> </span><br><span class="line"><span class="comment">//其它几种格式化方法</span></span><br><span class="line">now = moment().format(<span class="string">'L'</span>) <span class="comment">// 10/22/2016 </span></span><br><span class="line"><span class="built_in">console</span>.log(now);</span><br><span class="line"> </span><br><span class="line">now = moment().format(<span class="string">'LL'</span>) <span class="comment">// October 22, 2016</span></span><br><span class="line"><span class="built_in">console</span>.log(now);</span><br><span class="line"> </span><br><span class="line"><span class="comment">//格式化当前时间</span></span><br><span class="line">now = moment().format(<span class="string">'YYYY-MM-DD:HH:MM:SS'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(now);</span><br><span class="line"> </span><br><span class="line"><span class="comment">//获取这个月初时间</span></span><br><span class="line"><span class="keyword">let</span> startMonth = moment().startOf(<span class="string">'month'</span>).toDate();</span><br><span class="line"><span class="built_in">console</span>.log(startMonth);</span><br><span class="line"> </span><br><span class="line"><span class="comment">//获取今天开始的时间</span></span><br><span class="line"><span class="keyword">let</span> dayOfStart = moment().startOf(<span class="string">'day'</span>).toDate();</span><br><span class="line"><span class="built_in">console</span>.log(dayOfStart);</span><br><span class="line"> </span><br><span class="line"><span class="comment">//获取今天结束的时间</span></span><br><span class="line"><span class="keyword">let</span> dayOfEnd = moment().endOf(<span class="string">'day'</span>).toDate();</span><br><span class="line"><span class="built_in">console</span>.log(dayOfEnd);</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="comment">//获取＋n小时</span></span><br><span class="line"><span class="keyword">let</span> lateHour = moment().add(<span class="number">2</span>,<span class="string">'hour'</span>).toDate();</span><br><span class="line"><span class="built_in">console</span>.log(lateHour);</span><br><span class="line"> </span><br><span class="line"><span class="comment">//获取＋n小时</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'//获取-n小时'</span>)</span><br><span class="line"><span class="keyword">let</span> beforeHour = moment().subtract(<span class="number">2</span>,<span class="string">'hour'</span>).toDate();</span><br><span class="line"><span class="built_in">console</span>.log(beforeHour);</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="comment">//获取＋n天</span></span><br><span class="line"><span class="keyword">let</span> lateDay = moment().add(+<span class="number">5</span>,<span class="string">'day'</span>).toDate();</span><br><span class="line"><span class="built_in">console</span>.log(lateDay);</span><br><span class="line"> </span><br><span class="line"><span class="comment">//获取-n天</span></span><br><span class="line"><span class="keyword">let</span> beforeDay = moment().add(<span class="number">-5</span>,<span class="string">'day'</span>).toDate();</span><br><span class="line"><span class="built_in">console</span>.log(beforeDay);</span><br><span class="line"><span class="comment">//也可以表示为</span></span><br><span class="line">beforeDay = moment().subtract(<span class="number">5</span>,<span class="string">'day'</span>).toDate();</span><br><span class="line"><span class="built_in">console</span>.log(beforeDay);</span><br><span class="line"> </span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'//获取＋n月'</span>)</span><br><span class="line"><span class="keyword">let</span> lateMonth = moment().add(<span class="number">2</span>,<span class="string">'month'</span>).toDate();</span><br><span class="line"><span class="built_in">console</span>.log(lateHour);</span><br><span class="line"> </span><br><span class="line"><span class="comment">//获取＋n月</span></span><br><span class="line"><span class="keyword">let</span> beforeMonth = moment().subtract(<span class="number">2</span>,<span class="string">'month'</span>).toDate();</span><br><span class="line"><span class="built_in">console</span>.log(lateHour);</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="comment">//获取星期</span></span><br><span class="line"><span class="keyword">let</span> week = moment().format(<span class="string">'dddd'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(week);</span><br><span class="line"> </span><br><span class="line"><span class="comment">//获取到现在的年限 如果不满一年显示出具体几个月</span></span><br><span class="line"><span class="keyword">let</span> years = moment(<span class="string">'2020-12-31'</span>).fromNow();</span><br><span class="line"><span class="built_in">console</span>.log(years);</span><br></pre></td></tr></table></figure>
<h2 id="三、综合"><a href="#三、综合" class="headerlink" title="三、综合"></a>三、综合</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> moment <span class="keyword">from</span> <span class="string">'moment'</span></span><br><span class="line"></span><br><span class="line">DATE_TODAY: moment().format(<span class="string">'YYYY-MM-DD'</span>),<span class="comment">//今天</span></span><br><span class="line">DATE_YESTERDAY: moment().subtract(<span class="number">1</span>, <span class="string">'days'</span>).format(<span class="string">'YYYY-MM-DD'</span>), <span class="comment">// 昨天</span></span><br><span class="line">DATE_1_WEEK_BEFORE: moment().subtract(<span class="number">1</span>, <span class="string">'weeks'</span>).format(<span class="string">'YYYY-MM-DD'</span>),<span class="comment">// 一周前</span></span><br><span class="line">DATE_2_WEEKS_BEFORE: moment().subtract(<span class="number">2</span>, <span class="string">'weeks'</span>).format(<span class="string">'YYYY-MM-DD'</span>),<span class="comment">// 两周前</span></span><br><span class="line">DATE_3_WEEKS_BEFORE: moment().subtract(<span class="number">3</span>, <span class="string">'weeks'</span>).format(<span class="string">'YYYY-MM-DD'</span>),<span class="comment">// </span></span><br><span class="line">DATE_1_MONTH_BEFORE: moment().subtract(<span class="number">1</span>, <span class="string">'months'</span>).format(<span class="string">'YYYY-MM-DD'</span>),</span><br><span class="line">DATE_2_MONTH_BEFORE: moment().subtract(<span class="number">2</span>, <span class="string">'months'</span>).format(<span class="string">'YYYY-MM-DD'</span>),</span><br><span class="line">DATE_3_MONTHS_BEFORE: moment().subtract(<span class="number">3</span>, <span class="string">'months'</span>).format(<span class="string">'YYYY-MM-DD'</span>),</span><br><span class="line">DATE_1_YEAR_BEFORE: moment().subtract(<span class="number">1</span>, <span class="string">'years'</span>).format(<span class="string">'YYYY-MM-DD'</span>),</span><br><span class="line"></span><br><span class="line">DATE_3_MONTHS_AFTER: moment().add(<span class="number">3</span>, <span class="string">'months'</span>).format(<span class="string">'YYYY-MM-DD'</span>),</span><br><span class="line">DATE_1_YEAR_AFTER: moment().add(<span class="number">1</span>, <span class="string">'year'</span>).format(<span class="string">'YYYY-MM-DD'</span>),</span><br><span class="line"></span><br><span class="line">DATE_FIRST_DAY_OF_MONTH: moment().startOf(<span class="string">'month'</span>).format(<span class="string">'YYYY-MM-DD'</span>),</span><br><span class="line">DATE_LAST_DAY_OF_MONTH: moment().endOf(<span class="string">'month'</span>).format(<span class="string">'YYYY-MM-DD'</span>),</span><br><span class="line"></span><br><span class="line">DATE_7_DAYS_BEFORE: moment().subtract(<span class="number">7</span>, <span class="string">'days'</span>).format(<span class="string">'YYYY-MM-DD'</span>),</span><br><span class="line">DATE_30_DAYS_BEFORE: moment().subtract(<span class="number">30</span>, <span class="string">'days'</span>).format(<span class="string">'YYYY-MM-DD'</span>),</span><br><span class="line">DATE_90_DAYS_BEFORE: moment().subtract(<span class="number">90</span>, <span class="string">'days'</span>).format(<span class="string">'YYYY-MM-DD'</span>),</span><br><span class="line">DATE_100_DAYS_BEFORE: moment().subtract(<span class="number">100</span>, <span class="string">'days'</span>).format(<span class="string">'YYYY-MM-DD'</span>),</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 最近一个小时时间戳</span></span><br><span class="line"><span class="keyword">new</span> <span class="built_in">Date</span>(moment().add(<span class="number">1</span>, <span class="string">'h'</span>).format(<span class="string">'YYYY-MM-DD hh:mm:ss'</span>)).getTime()</span><br><span class="line"></span><br><span class="line"><span class="comment">// 格式化时间戳为日期 2018-12-06:12:21</span></span><br><span class="line">moment(时间戳 * <span class="number">1000</span>).format(<span class="string">'YY-MM-DD:HH:mm:ss'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 计算时间当前时间和最近一小时时间差 差分钟</span></span><br><span class="line"><span class="keyword">const</span> oneHourBefore = <span class="keyword">new</span> <span class="built_in">Date</span>(moment().add(<span class="number">1</span>, <span class="string">'h'</span>).format(<span class="string">'YYYY-MM-DD hh:mm:ss'</span>)).getTime()</span><br><span class="line"><span class="keyword">const</span> currTime = <span class="built_in">Date</span>.now()</span><br><span class="line"><span class="keyword">const</span> minute = moment(currTime).diff(oneHourBefore), <span class="string">'minute'</span>)</span><br></pre></td></tr></table></figure>
<h2 id="四、其他"><a href="#四、其他" class="headerlink" title="四、其他"></a>四、其他</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * [getTimeList description] 生成时间列表 </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param  <span class="type">&#123;[type]&#125;</span> </span>hours [description] 小时 </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param  <span class="type">&#123;[type]&#125;</span> </span>step  [description] 分段 </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return <span class="type">&#123;[type]&#125;</span>       </span>[description] 时间段列表 </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getTimeList</span>(<span class="params">hours, step</span>)</span>&#123;  </span><br><span class="line">    <span class="keyword">var</span> minutes = <span class="number">60</span>  </span><br><span class="line">    <span class="keyword">var</span> timeArr = []  </span><br><span class="line">    hours = hours  </span><br><span class="line">    step = step   </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; hours; i++)&#123;  </span><br><span class="line">        <span class="keyword">var</span> str = <span class="string">''</span>  </span><br><span class="line">        <span class="keyword">if</span>(i &lt; <span class="number">10</span>) &#123;  </span><br><span class="line">            str = <span class="number">0</span> + <span class="string">''</span> + i  </span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;  </span><br><span class="line">            str = <span class="string">''</span> + i  </span><br><span class="line">        &#125;  </span><br><span class="line">  </span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; minutes; j++) &#123;  </span><br><span class="line">            <span class="keyword">if</span>(j % step == <span class="number">0</span>)&#123;  </span><br><span class="line">                <span class="keyword">var</span> s = j &lt; <span class="number">10</span> ? <span class="string">':'</span> + <span class="number">0</span> + <span class="string">''</span> + j : <span class="string">':'</span> +  j;  </span><br><span class="line">                s = str + s  </span><br><span class="line">                timeArr.push(s)  </span><br><span class="line">            &#125;  </span><br><span class="line">        &#125;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="keyword">return</span> timeArr;  </span><br><span class="line">  </span><br><span class="line">&#125;  </span><br><span class="line">getTimeList(<span class="number">12</span>, <span class="number">15</span>)</span><br></pre></td></tr></table></figure>
<h2 id="五、更多参考"><a href="#五、更多参考" class="headerlink" title="五、更多参考"></a>五、更多参考</h2><ul>
<li><a href="https://www.jianshu.com/p/9c10543420de" target="_blank" rel="noopener">Moment.js常见用法总结</a></li>
</ul>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2018/12/06/lodash-api/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2018/12/20/quickapp-oppo-openid/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
